package com.tf_cut.backend.api.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.backend.api.pojo.ApiPaidRes;
import com.tf_cut.backend.api.pojo.SinglePaid;
import com.tf_cut.backend.api.service.ApiService;
import com.tf_cut.backend.person.service.PersonService;
import com.tf_cut.backend.person.vo.PersonAndAccountVo;
import com.tf_cut.backend.seller.service.SellerAccountService;
import com.tf_cut.backend.seller.service.SellerRefAgentService;
import com.tf_cut.backend.seller.vo.SellerAndAccountVo;
import com.tf_cut.backend.seller.vo.SellerRefAgentVo;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backend/api")
@Slf4j
public class ApiController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private SellerAccountService sellerAccountService;
    
    @Autowired
    private PersonService personService;
    
	@Autowired
	private SellerRefAgentService sellerRefAgentService;

    @PostMapping("/getSellerInfo")
    public String getSellerInfo(String appKey){
        return apiService.getSellerInfo(appKey);
    }

    @PostMapping("/singlePaid")
    public String singlePaid(String json,String platform,String merchantNumber){
        SinglePaid paid = JSON.parseObject(json,SinglePaid.class);
        String res = apiService.singlePaid(paid,platform,merchantNumber);
        Map<String,String> resMap = new HashMap<>();
        if("noServiceInfo".equalsIgnoreCase(res)){
            resMap.put("code","2");
            resMap.put("msg","没有可用的服务商");
        }else if ("noAgentInfo".equalsIgnoreCase(res)){
            resMap.put("code","3");
            resMap.put("msg","没有可用的代理商");
        }else {
            resMap.put("code","0");
            resMap.put("msg","成功");
            resMap.put("data",res);
        }

        return JSON.toJSONString(resMap);
    }

    @PostMapping("/getBalance")
    public String getBalance(String sellerId){
        SellerAndAccountVo sellerAndAccountVo = SellerAndAccountVo.builder().sellerId(sellerId).build();
        SellerAndAccountVo retSellerAndAccountVo = sellerAccountService.findSellerAccountByCondition(sellerAndAccountVo);
        return retSellerAndAccountVo == null ? "0" : retSellerAndAccountVo.getAccountTotal().toString();
    }

    @PostMapping("/qryRecord")
    public String qryRecord(String json){
        List<String> noList = JSON.parseArray(json,String.class);
        List<ApiPaidRes> resList = apiService.qryRecord(noList);
        resList.forEach(api -> {
            switch (api.getStatus()){
                case "1":
                    api.setStatus("2");
                    break;
                case "2":
                    api.setStatus("3");
                    break;
                default:
                    api.setStatus("1");
                    break;
            }
        });
        return JSON.toJSONString(resList);
    }
    
    
    @PostMapping("/findbyqueryPerson")
    public PersonAndAccountVo findbyqueryPerson(@RequestParam("identityCardNum")String identityCardNum){
    	return personService.findbyqueryPerson(identityCardNum);
    }
    
    
    @PostMapping("/queryBySellerName")
    public Integer queryBySellerName(@RequestParam("sellerName") String sellerName,@RequestParam("sellerCompanyName")String sellerCompanyName){
    	return sellerAccountService.queryBySellerName(sellerName,sellerCompanyName);
    }
    
    @PostMapping("/queryBySellerRefAgent")
    public SellerRefAgentVo queryBySellerRefAgent(@RequestParam("agentNumber") String agentNumber){
    	return sellerRefAgentService.queryBySellerName(agentNumber);
    }
    
    
    @PostMapping("/queryBySellerId")
    public SellerAndAccountVo queryBySellerId(@RequestParam("appIdKey") String appIdKey){
    	return sellerAccountService.queryBySellerId(appIdKey);
    }
    
    @PostMapping("/findByType")
    public String findByType(@RequestParam("serviceType") String serviceType){
    	return sellerAccountService.findByType(serviceType);
    }
    
}
